package com.distribution.system.manager.zoningcode;

import com.distribution.system.mapper.CmccDaZoningCodeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author Liping Huo
 * @date 2023/5/23 1:26
 */
@Service
public class DefaultMateZoningCode implements MateZoningCode {
    final
    CmccDaZoningCodeMapper cmccDaZoningCodeMapper;

    public DefaultMateZoningCode(CmccDaZoningCodeMapper cmccDaZoningCodeMapper) {
        this.cmccDaZoningCodeMapper = cmccDaZoningCodeMapper;
    }

    @Override
    public String mate(String address) {
        if (isNumber(address)) {
            return address;
        }
        String zoningCode = cmccDaZoningCodeMapper.mateCountyZoningCode(address);
        if (Objects.isNull(zoningCode)) {
            zoningCode = cmccDaZoningCodeMapper.mateCityZoningCode(address);
        }
        if (Objects.isNull(zoningCode)) {
            zoningCode = cmccDaZoningCodeMapper.mateProvinceZoningCode(address);
        }
        return zoningCode;
    }

    /**
     * 判断是否为数字可以使用工具类 StringUtils
     * 通过方法 isNumeric 进行判断是否为数字
     *
     * @param str
     * @return
     */
    private static boolean isNumber(String str) {
        return StringUtils.isNumeric(str);
    }

}
